#pragma once
#include <fstream>
#include <iostream>
#include <string>
#include <istream>
#include <sstream>
#include <map>
#include <queue>
using namespace std;
namespace Tentamen
{
	struct data_pq
	{
		string initierandeIP;
		int antalUppkopplingar;

		data_pq()
		{
			initierandeIP = "";
			antalUppkopplingar = -1;
		}

		data_pq(string s, int a)
		{
			this->initierandeIP = s;
			this->antalUppkopplingar = a;
		}


		friend bool operator<(const data_pq &d1,const data_pq &d2)
		{
			return d2.antalUppkopplingar > d1.antalUppkopplingar;
		}
	};

	class IPstoring
	{
	private:
		map<string, int> IPmap;
	public:
		IPstoring()
		{
		}

		void ReadIPs(string fileName)
		{
			string str, strRow;
			ifstream fin;

			try
			{
				fin.open(fileName);

				while (getline(fin, strRow))
				{
					istringstream iss(strRow);

					getline(iss, str, ' ');

					auto it = IPmap.find(str);

					if (it != IPmap.end())
					{
						it->second++;
					}

					else
					{
						IPmap.emplace(str, 1);
					}	
				}

				fin.close();
			}

			catch (exception e)
			{
				cerr << "Exception: " << fileName << endl;
				cerr << e.what() << endl;
			}
			
		}

		void AntalUppkopplingar(string ip)
		{
			auto it = IPmap.find(ip);
			cout << "IP: " << it->first << ", antal uppkopplingar: " << it->second << endl;
		}

		void TioMestFrekventa()
		{
			priority_queue <data_pq> pq;
			
			for (auto it = IPmap.begin(); it != IPmap.end(); it++)
			{
				data_pq temp(it->first, it->second);
				pq.push(temp);
			}

			for (int i = 1; i <= 10; i++)
			{
				cout << i << ": " << pq.top().initierandeIP << ", " << pq.top().antalUppkopplingar << endl;
				pq.pop();
			}
			
		}

		~IPstoring()
		{
		}
	};



}
